<?php

namespace App\Jobs\System;
// 加载采集环境

use App\Wen\Utils\FileUtils;
use App\Wen\Utils\ImageUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\StrUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class SettingImgToJpgJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $keys = [];
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($keys)
    {
        $this->keys = $keys;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->keys)){
            return;
        }
        foreach ($this->keys as $key){
            $img_url = Settings::get($key);
            if(_empty_($img_url)){
                continue;
            }
            if(StrUtils::endsWith($img_url, '.jpg')){
                continue;
            }
            $path = ImageUtils::upload_url_img($img_url . '?imageMogr2/format/jpg/interlace/0', md5(uniqid()) . '.jpg');
            if($path && $path['url']){
                Settings::set($key, $path['url'], true);
                FileUtils::force_del_url_file($img_url);
            }
        }
    }
}
